window.onload = function() {
    // 所有比赛的下拉菜单
    var flag = 0;
    $("#all-match .fa-caret-down").on('click',function() {
        if(flag===0){
            $(".down-list").css("visibility","visible");
            flag++;
        }else {
            $(".down-list").css("visibility","hidden");
            flag--;            
        }
    });
    // 导航栏切换
    $('.works>ul>li').on("click",function() {
        var liList = $('.works>ul>li');
        var divList = $('.appreciation-content');
        console.log($(this))
        if($(this).attr('id')!='else-choose') {
            for(var i=0;i<divList.length;i++) {
                    divList[i].style.display = 'none';
                    liList[i].className = "";                
            }

            $(this).addClass('active-li');      
            var activeLi = $('.active-li');
            var targetId = activeLi.attr('data-target');
                var targetDiv = document.getElementById(targetId);
                targetDiv.style.display = "block";
        }


    });

    //搜索框设计：
    // 1. 搜索框状态变化时，异步发送请求，提示搜索结果
    // 2. 当点击搜索匹配结果的时候，将结果值放到搜索栏中
    //api ：
    //  onchange 函数，检测输入框状态变化。
    //  showResult 函数， 更新搜索结果
    function onchange(ele){
        var value = ele.value;
        console.log(value);
        if(value) {
            $.ajax({
                type: 'get',
                async: false,
                url: 'http://mobile.goamob.com/index.php/Community/search/k/' + value,
                // data: "params=" + value,
                dataType: 'json',
                cache: false,
                success: function(data){
                    // console.log("hi");
                    console.log(data);
                    updateList(data);
                },
                error: function(error){
                    alert("not found");
                }
            });    
        }
        
    }

    function updateList(data) {
        console.log("success");
        console.log(data.data[0]);
        console.log($("ul.search-select"));
        if(data) {
            for(var i = 0; i < data.data.length; i++){
                var list_item = data.data[i].username;
                var main_html = '<li><a href="">' + list_item + '</a></li>'
                $(".search-select").html(main_html);
                $(".search-select li").css({
                    // "position": "absolute",
                    "width": "100%",
                    "text-align": "center",
                    "z-index": 2
                });
                $(".search-select").css("display", "block");
            }

        } else {
            $("search-select").html("nothing");
        }
    }
    // console.log("search");
    $("#else-choose input").on("input", function(){
        onchange(this);
    });

    $(".search-select").on("click", function (event){
        console.log(event.target);
        $("#else-choose").val(event.target.html());
    });

}